<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            <!-- 扫码采集 -->
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">当前操作人:</div>
                    <input type="text" class="aui-input" id="empName" disabled style="width: 80%;" />
                    <i class="aui-btn aui-btn-info aui-input-addon aui-iconfont" onclick="takeQuitPostClick()">上岗</i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">过件数量:</div>
                    <div class="aui-list-item-label">合格:</div>
                    <input type="text" class="aui-input" id="hgQty" disabled style="width: 25%;" />
                    <div class="aui-list-item-label">不合格:</div>
                    <input type="text" class="aui-input" id="bhgQty" disabled style="width: 25%;" />
                    <i class="aui-btn aui-btn-info aui-input-addon aui-iconfont" onclick="clearRecord()">清空</i>
                </div>
            </li>
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料编码:</div>
                    <input type="text" class="aui-input" disabled="true" id="mrlCode" placeholder="请扫描产品条码">
                </div>
            </li> -->
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料编码:</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描产品条码" id="mrlCode" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">报工数量:</div>
                    <input type="text" class="aui-input" id="qty" placeholder="请输入报工数量">
                </div>
            </li>
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">蓝牙设备</div>
                    <select id="bluetooth">
                    </select>
                </div>
            </li> -->
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <!-- <div class="aui-btn aui-btn-info aui-btn-block" onclick="bluetoothConnect()">蓝牙连接</div> -->
        <div style="width:100%;margin-top:2%;"></div>
        <!-- <div class="aui-btn aui-btn-info aui-btn-block" onclick="print()">打印</div> -->
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="onReport()">报工</div>

        <div style="width:100%;margin-top:2%;"></div>

        <ul class="ui-selectee" id="tbodyHtmlS" style="margin-bottom:12%;">

        </ul>

    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">

    var equipUniqueId = "";   //设备唯一号
    var workCenterCode = "";  //生产线编码
    var workCellCode = "";    //工位编码
    var opCode = "";          //工序编码
    var ruleCode = "";          //条码规则
    var scancodeWeb = "";
    var teamCode = "";        //班组编码
    var teamName = "";        //班组名称
    var shiftCode = "";       //班次编码
    var shiftName = "";       //班次名称
    var proddate = "";        //生产日期
    var pageParam = {};
    var mrlCode = "";//物料编码
    var mrlName = "";//物料名称
    var arrSave = [];
    var isCheckPersonnelSkill = "";//是否开启人员上岗技能校验
    

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //初始化查询仓库下拉框
    apiready = function () {

        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');
        $("#mrlCode").focus();
        idSelector = "mrlCode";
        //1:查询产线工位信息
        searchWorkCenterData();

        //获取蓝牙列表
        //getBluetoothCombox();

    }

    //物料扫描校验
    function getMrl(code, inputId) {

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getMdMrlListGu";
        var data = {
            values: {
                mrlCode: code,
                un: un,
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {

                    mrlCode = code;
                    mrlName = myobj.mrlName;
                    $("#mrlCode").val(mrlCode + ":" + mrlName);
                    if(myobj.number!=null && myobj.number !=undefined){
                        $("#qty").val(myobj.number)
                    }else{
                        $("#qty").val("")
                    }
                    $("#qty").select();
                    idSelector = "qty";
                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                    $("#mrlCode").val("");
                    $("#mrlCode").focus();
                    $("#mrlCode").select();
                }
            }
        });
    }


    function setTable(list) {
        var html = "";
        for (var i = 0; i < list.length; i++) {
            html += "<li class='sortable ui-selectee ui-sortable'>";
            html += "<ul class='aui-list aui-media-list ui-selectee'>";
            html += "<li class='aui-list-item  ui-selectee'>";
            html += "<div class='aui-media-list-item-inner ui-selectee'>";
            html += "<div class='aui-list-item-inner ui-selectee'>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>箱号：</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].boxnum + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料编码：</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].mrlCode + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>批次号：</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].lotCode + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>报工数量：</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].bgQty + "</div>";
            // html += "<div class='aui-list-item-right aui-btn ui-selectee' style='background:red;color:#f5f5f5;' onclick='bdPrint(\"" + list[i].boxnum + "\");'>补打</div>";
            html += "</div>";

            html += "</div>";
            html += "</li>";
            html += "</ul>";
            html += "</li>";
        }
        $api.html($api.byId("tbodyHtmlS"), html);
    };

    //打印
    function onReport() {
        var qty = $("#qty").val();

        //var address = $("#bluetooth").val();

        //物料号非空校验
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            api.toast({
                msg: '物料编码不能为空！',
                duration: 3000
            });
            return;
        }
        //报工数量非空校验
        if (qty == "" || qty == null || qty == undefined || parseFloat(qty) <= 0) {
            api.toast({
                msg: '报工数量必须大于0！',
                duration: 3000
            });
            return;
        }

        // if (address == null || address == "" || address == undefined) {
        //     api.toast({
        //         msg: "请选择蓝牙！"
        //     });
        //     return;
        // }
        var empName=$("#empName").val();

        // localStorage.setItem('bluetooth', address);

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/pdaPrint";
        var data = {
            values: {
                scanCode: mrlCode, //物料编码
                qty: qty, //报工数量
                workCellCode: workCellCode,
                lineCode: workCenterCode,
                shiftCode: shiftCode,
                shiftName: shiftName,
                proddate: proddate,
                teamCode: teamCode,
                opCode: opCode,
                mrlCode: mrlCode,
                mrlName: mrlName,
                equipUniqueId: equipUniqueId,
                empName:empName,
                un: un
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var model = {
                        boxnum: myobj.boxnum, //
                        tm: myobj.tm, //
                        bgQty: qty, //
                        lotCode: myobj.lotCode,
                        mrlCode: mrlCode,
                    }
                    arrSave.push(model);
                    setTable(arrSave);


                    // //返回json数据
                    // var json = myobj.json;

                    // //调用PDA打印
                    // printPDA(json, address);
                    
                     //查询过点
                     getPassPoint();

                } else {

                    $("#tm").val("");
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            } else {

                $("#tm").val("");
                alert(JSON.stringify(err));
            }
        });
    }

    //查询数据
    function refresh() {

        //1:查询当前操作人员信息
        searchDutyPersonData();

        //查询过点
        getPassPoint();
    }


    //打印
    function printPDA(json, address) {
        console.log(11111)
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.printerPlatform({
            json: json, //打印数据JSON
            address: address //蓝牙地址
        }, function (ret, err) {
            if (ret) {

                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });

                    //清空表单数据
                    $("#mrlCode").val("");
                    mrlName = ''
                    mrlCode = ''
                    $("#qty").val("");

                     //查询过点
                     getPassPoint();


                } else {

                    $("#tm").val("");
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                $("#tm").val("");
                alert(JSON.stringify(err));
            }
        });
    }


    //打印
    function bdPrint(boxnum) {

        var address = $("#bluetooth").val();


        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙！"
            });
            return;
        }

        localStorage.setItem('bluetooth', address);

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/pdaBdPrint";
        var data = {
            values: {
                boxnum: boxnum,
                un: un
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {

                    //返回json数据
                    var json = myobj.json;


                    //调用PDA打印
                    printPDA(json, address);


                } else {

                    $("#tm").val("");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {

                $("#tm").val("");
                alert(JSON.stringify(err));
            }
        });
    }

    //查询蓝牙设备
    function getBluetoothCombox() {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.searchBluetooth({}, function (ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    var list = ret.list;
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("bluetooth"), html);

                    //取缓存中蓝牙设备打印机
                    var bluetooth = localStorage.getItem('bluetooth');
                    if (bluetooth != null && bluetooth != undefined && bluetooth != "") {
                        $("#bluetooth").val(bluetooth);
                    }

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //上岗
    function takeQuitPostClick() {
        if (opCode == null || opCode == "" || opCode == undefined) {
            api.toast({
                msg: "唯一号【" + equipUniqueId + "】未配置工序信息",
                duration: 3000
            });
            return;
        }
        api.openWin({
            name: "productionSglg",
            url: 'productionSglg.html',
            pageParam: pageParam,
        });
    }
    //构造传参map
    function createPageParam() {
        pageParam = {
            workCenterCode: workCenterCode,
            workCellCode: workCellCode,
            opCode: opCode,
            ruleCode: ruleCode,
            teamCode: teamCode,
            teamName: teamName,
            shiftCode: shiftCode,
            shiftName: shiftName,
            proddate: proddate,
            rootName: "paScanMrlCode",
            name: "paScanMrlCode",
            sgFlag: '0',//上岗标识：0刷卡，1勾选
            isCheckPersonnelSkill:isCheckPersonnelSkill
        }
    }

    //清空过点
    function clearRecord() {
        if (opCode == null || opCode == "" || opCode == undefined) {
            api.toast({
                msg: "唯一号【" + equipUniqueId + "】未配置工序信息",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/clearPassPoint";
        var data = {
            values: {
                epUniquecode: equipUniqueId,
                lineCode: workCenterCode,
                stationCode: workCellCode,
                opCode: opCode,
                un: un,
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {

                    //刷新过点数据显示
                    getPassPoint();

                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }



    //蓝牙连接
    function bluetoothConnect() {
        var address = $("#bluetooth").val();
        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙"
            });
            return;
        }
        // console.log(address);
        // api.showProgress({
        //     title: '连接中...',
        // });
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.connectBluetooth({
            address: address,
        }, function (ret, err) {
            if (ret) {
                // api.hideProgress();
                if (ret.code == "S") {
                    api.toast({
                        msg: ret.message
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }


    //查询产线工位信息
    function searchWorkCenterData() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getOpCodeByUniCode";
        var data = {
            values: {
                clientCode: equipUniqueId,
                un: un,
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {

                    //标题
                    document.getElementById("title").innerHTML = myobj.opCode + "压铸件";
                    workCenterCode = myobj.workCenterCode;
                    workCellCode = myobj.workCellCode;
                    opCode = myobj.opCode;
                    ruleCode = myobj.ruleCode;
                    isCheckPersonnelSkill=myobj.isCheckPersonnelSkill;

                    if (opCode != null && opCode != "") {

                        //1:查询当前操作人员信息
                        searchDutyPersonData();

                      
                    }

                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //查询过点
    function getPassPoint() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var operator = $("#empName").val();
        var url = serverUrl + "/app/getPassPoint";
        var data = {
            values: {
                lineCode: workCenterCode,
                stationCode: workCellCode,
                opCode: opCode,
                epUniquecode:equipUniqueId,
                operator:operator,
                un: un,
            }
        };
       
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {

                    //合格数
                    document.getElementById("hgQty").value = myobj.qualifyQTy;
                    //不合格数
                    document.getElementById("bhgQty").value = myobj.unQualifyQTy;
                    //返修数数
                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }


    //查询人员信息
    function searchDutyPersonData() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getProductionAssemblyTake";
        var data = {
            values: {
                workCenterCode: workCenterCode,
                opCode: opCode,
                workCellCode:workCellCode,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                console.log(myobj.code)

                if (myobj.code == 0) {
                    console.log(myobj.empName)
                    document.getElementById("empName").value = myobj.empName;
                    teamCode = myobj.teamCode;
                    teamName = myobj.teamName;
                    shiftCode = myobj.shiftCode;
                    shiftName = myobj.shiftName;
                    proddate = myobj.proddate;


                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });

                    //清空人员信息
                    clear();
                }
                  //查询过点
                  getPassPoint();
                //5:构造参数
                createPageParam();
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //清空人员上岗信息
    function clear() {
        document.getElementById("empName").innerHTML = "";
        teamCode = "";
        teamName = "";
        shiftCode = "";
        shiftName = "";
        proddate = "";
    }
    //查找当前工序最后一个件信息
    function searchWorkLast() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getProductionAssemblyTake";
        var data = {
            values: {
                workCenterCode: workCenterCode,
                opCode: opCode,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

    }



    //扫描之后条码解析
    function tmSplit(sm) {
        console.log("tmSplit:" + JSON.stringify(sm));
        var arr = sm.split(",");
        console.log("split:" + JSON.stringify(arr));
        if (arr.length == 1) {
            $("#boxnum").val(sm);
            doSearch();
        } else {
            //直接解析
            if (scanRule == "old") {

                //本地解析
                localAnalyze(sm);

            } else {

                //后台解析
                postAnalyze(sm);
            }
        }
    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        if (arr[9] == null || arr[9] == undefined) {
            $("#boxnum").val("");
        } else {
            $("#boxnum").val(arr[9]);
        }
        doSearch();

    }

    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    $("#boxnum").val(map["boxnum"]);
                    //自动保存
                    doSearch();
                } else if (myobj.code == '1') {

                    $("#boxnum").val("");
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    // //扫描
    // function doScan() {
    //     var moduleScanner = api.require('moduleScanner');
    //     var params = {
    //         top: 150, //扫描窗口的上边距
    //         width: 250, //扫描窗口的宽
    //         height: 250, //扫描窗口的高
    //         useColor: false, //是否使用自己设定的颜色(startColor和endColor),
    //         //如果不使用设定的颜色就使用默认的那张网格图
    //         startColor: '#0F08C8FF', //渐变起始颜色
    //         endColor: '#7F08C8FF', //渐变结束颜色
    //         animTime: 2200, //扫描动画时间,单位为毫秒
    //     };
    //     moduleScanner.toScannerActivityForResult(params, function (ret, err) {

    //         //条码
    //         // var arr = ret.split(",");
    //         $("#boxnum").val(ret);
    //         // if (arr[9] != null && arr[9] != "" && arr[9] != undefined) {
    //         doSearch();
    //         // }
    //     });
    // }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //1：箱号
            if (idSelector == 'boxnum') {

                $("#boxnum").val(ret);
                doSearch();

                //24：调入库位
            } else if (idSelector == 'mrlCode') {

                $("#mrlCode").val(ret);
                //查询物料
                getMrl(ret, idSelector);


            }
        });
    }

    $("#mrlCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#mrlCode").val(sm);
            //查询物料
            getMrl(sm, "mrlCode");
        }
    });

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>